{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "80cc104e-d9da-4a36-9c33-56c4e4325ea4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from lambda_or import lambda_or, neglog10_p_from_z\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7996dc8f-6fd6-497e-b9d2-e0f40b8efbec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# rows = X in {1,0}\n",
    "# cols = Y~ in {1,0}\n",
    "tilde_counts = np.array([\n",
    "    [20,  30],\n",
    "    [10, 240]\n",
    "], dtype=float)\n",
    "\n",
    "# naive OR\n",
    "a, b = tilde_counts[0]\n",
    "c, d = tilde_counts[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dbf321d4-59f2-4cc7-be08-57e115e447c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "naive_or = (a * d) / (b * c)\n",
    "naive_log_or = np.log(naive_or)\n",
    "naive_se = np.sqrt(1/a + 1/b + 1/c + 1/d)\n",
    "naive_z = naive_log_or / naive_se\n",
    "naive_neglog10_p = neglog10_p_from_z(abs(naive_z))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d41cd345-b88d-41f2-8d96-24826cc2f1be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Naive OR: 16.0\n",
      "Naive log OR: 2.772588722239781\n",
      "Naive z: 6.403019380549032\n",
      "Naive -log10(p): 9.817203589799401\n",
      "\n",
      "Lambda-OR: 19.347225882973113\n",
      "Log Lambda-OR: 2.9625490439769875\n",
      "SE: 1.512969574639501\n",
      "z: 1.9581021942776882\n",
      "Lambda -log10(p): 1.2991403944258946\n",
      "lambda used: 10.0\n",
      "\n",
      "Corrected contingency table:\n",
      "[[ 1.81144781  2.73737374]\n",
      " [ 0.75420875 22.05050505]]\n"
     ]
    }
   ],
   "source": [
    "# lambda-OR\n",
    "res = lambda_or(\n",
    "    tilde_counts=tilde_counts,\n",
    "    p_sel=0.92,\n",
    "    q_sel=0.88,\n",
    "    n_val=1000\n",
    ")\n",
    "\n",
    "print(\"Naive OR:\", naive_or)\n",
    "print(\"Naive log OR:\", naive_log_or)\n",
    "print(\"Naive z:\", naive_z)\n",
    "print(\"Naive -log10(p):\", naive_neglog10_p)\n",
    "\n",
    "print(\"\\nLambda-OR:\", np.exp(res.log_or))\n",
    "print(\"Log Lambda-OR:\", res.log_or)\n",
    "print(\"SE:\", res.se)\n",
    "print(\"z:\", res.z)\n",
    "print(\"Lambda -log10(p):\", res.neglog10_p)\n",
    "print(\"lambda used:\", res.lambda_used)\n",
    "\n",
    "print(\"\\nCorrected contingency table:\")\n",
    "print(res.counts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef2d66b5-c9dc-46ba-8ba3-6c71c8c5df45",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a9a3526-54d6-420e-8581-abf180f6e69f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
